package cn.com.ourclass.webviewnew.web.js;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.google.zxing.integration.android.IntentIntegrator;
import com.hc.framework.view.PhotoViewActivity;

import java.util.ArrayList;

import cn.com.ourclass.webviewnew.web.WebViewNewActivity;
import cn.com.ourclass.webviewnew.web.tools.PhotoSelectActivity;
import cn.com.ourclass.webviewnew.web.tools.ScanningCodeActivity;


/**
 * Created by HuHaiqin on 2017/11/16.
 */

public class MiniwebInterface {

    private WebViewNewActivity context;

    public MiniwebInterface(WebViewNewActivity context) {
        this.context = context;
    }


    @JavascriptInterface
    public void open(String url,String statusBarColor){
        context.loadUrl(context,url,statusBarColor); //打开一个新的webview
    }

    @JavascriptInterface
    public void close(){
        //关闭当前的webview
        context.close();
    }
    /**
     * 点击网页里面的图片
     * @param imgUrl 所有图片的地址用#_#分割,确保误分割
     * @param curUrl 当前图片地址
     */
    @JavascriptInterface
    public void imageClick(String imgUrl, String curUrl) {
        ArrayList<String> images = new ArrayList<>(); //图片参数
        String[] imgs = imgUrl.split("#_#");
        int index = 0 ;
        for(int i = 0 ; i < imgs.length ; i++){
            images.add(imgs[i]);
            if(imgs[i].equals(curUrl)){
                index = i;
            }
        }

        Intent intent = new Intent(context, PhotoViewActivity.class);
        intent.putExtra(PhotoViewActivity.CURRENT_POINT_KEY,index);
        intent.putStringArrayListExtra(PhotoViewActivity.IMAGE_URL_LIST_KEY,images);
        context.startActivity(intent);
    }

    @JavascriptInterface
    public void scan(){
        //扫描二维码
        new IntentIntegrator(context)
                .setOrientationLocked(false)
                .setCaptureActivity(ScanningCodeActivity.class) // 设置自定义的activity是CustomActivity
                .initiateScan(); // 初始化扫描
    }

    @JavascriptInterface
    public void mylog(String log){
        Log.e("h5",log);//打印日志
    }

    @JavascriptInterface
    public void linkClick(String url,String needTitle) {
        if (!TextUtils.isEmpty(url)) {
//            if(needTitle.equals("0")){
//                WebViewActivity.loadUrl(context, url, false,false);
//            }else{
//                WebViewActivity.loadUrl(context, url, false,true);
//            }
            //点击link
            Log.e("haiqin",url);
        }
    }

    @JavascriptInterface
    public void uploadImages(){
        //上传图片的接口
        Intent intent = new Intent(context, PhotoSelectActivity.class);
        //startActivityForResult(intent, -1);
        context.startActivityForResult(intent, PhotoSelectActivity.REQUEST_PHOTO_CODE);
    }

    //分享 标题，内容，图片路径，跳转链接
    @JavascriptInterface
    public void share(String title,String content,String imgpath,String url){
        cn.sharesdk.onekeyshare.OnekeyShare oks = new cn.sharesdk.onekeyshare.OnekeyShare();
//        oks.setPlatform();
        oks.setTitle(title);
        // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
        oks.setText(content);
        oks.setUrl(url);
        oks.setImageUrl(imgpath);
        // 启动分享GUI
        oks.show(context);
    }

}
